package eu.expressis.eiaf.core.infrastructure.util;

import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.spi.PersistenceUnitInfo;

import org.hibernate.cfg.Configuration;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(defaultRollback = true)
public class ExportDatabaseSchema {
  @Resource(name = "&entityManagerFactory")
  private LocalContainerEntityManagerFactoryBean entityManagerFactory;

  @Test
  public void exportDatabaseSchema() {
    PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo();
    Map jpaPropertyMap = entityManagerFactory.getJpaPropertyMap();
    Configuration configuration = new Ejb3Configuration().configure(persistenceUnitInfo, jpaPropertyMap)
        .getHibernateConfiguration();
    SchemaExport schema = new SchemaExport(configuration);
    schema.setOutputFile("target/schema.sql");
    schema.create(false, false);
  }
}